package com.atguigu.interactive.v2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AirCondition {
    private int number = 0;
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while(number !=0){
                condition.await();
            }

            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public  void decrement() throws InterruptedException {
        lock.lock();
        try{
            while(number == 0)
            {
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}
